lensTp<-function(t,te,tm){return((t-tm)/te)}
lensU<-function(t,te,tm,b){return(sqrt(b^2+lensTp(t,te,tm)^2))}
lensBlend<-function(f,a){return(f * (a - 1.0) + 1.0)}
lensSLAmp<-function(t,b,te,tm,f){u<-lensU(t,te,tm,b);amp<- (u * u + 2.0) / u / sqrt(u * u + 4.0);return(lensBlend(f,amp))}
lensMag<-function(a,m0){return(-2.5*log10(a)+m0)}
lensMagFunc<-function(par){return(lensMag(lensSLAmp(par[1],par[2],par[5],par[6],par[3]),par[4]))}
myChiSq<-function(x1,x2,err){return(sum(((x1-x2)/err)^2))}
lensChiSq<-function(t,y,err,b,f,m0,te,tm) {ymodel<-lensMag(lensSLAmp(t,b,te,tm,f),m0);return(myChiSq(y,ymodel,err))}
lensFit<-function(p) {return(lensChiSq(xdata,ydata,errdata,p[1],p[2],p[3],p[4],p[5]))}
